package io.babystar.modules.business.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;
import io.babystar.modules.business.dao.CkCategoryDao;
import io.babystar.modules.business.entity.CkCategoryEntity;
import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.service.CkCategoryService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("ckCategoryService")
public class CkCategoryServiceImpl extends ServiceImpl<CkCategoryDao, CkCategoryEntity> implements CkCategoryService {

    /**
     * 负责一级分类的查询和分类名字的模糊查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //初始化变量：pid可以为0，代表一级分类
        Integer pid = null;
        //初始化变量：模糊查询分类
        String category = "";
        //pid非空判断
        if (params.get("pid") != null && !"".equals(params.get("pid").toString())) {
            pid = Integer.parseInt(params.get("pid").toString());
        }
        //模糊查询非空判断
        if (params.get("category") != null && !"".equals(params.get("category").toString())) {
            category = params.get("category").toString();
        }

        IPage<CkCategoryEntity> page = this.page(
                new Query<CkCategoryEntity>().getPage(params),
                new QueryWrapper<CkCategoryEntity>().like(category != null && !"".equals(category), "category", category)
                        .eq(pid != null, "pid", pid)
        );
        return new PageUtils(page);
    }

    /**
     * 负责二级分类的查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage2(Map<String, Object> params) {
        Integer pid = null;
        if (params.get("pid") != null && !"".equals(params.get("pid").toString())) {
            pid = Integer.parseInt(params.get("pid").toString());
        }
        IPage<CkCategoryEntity> page = this.page(
                new Query<CkCategoryEntity>().getPage(params),
                new QueryWrapper<CkCategoryEntity>()
                        .gt(pid == null, "pid", 0)
                        .eq(pid != null, "pid",pid)
        );

        return new PageUtils(page);
    }

}